package com.gosophia.conferenceManagement.test.unit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.mapping.Array;
import org.hibernate.persister.entity.EntityPersister;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.confPreparation.entity.ConfPreparationJobData;
import com.gosophia.conferenceManagement.confPreparation.entity.NotificationFeedbackList;
import com.gosophia.conferenceManagement.confPreparation.entity.ParticipatingNameList;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.entity.Mission;
import com.gosophia.userManagement.entity.Permission;
import com.gosophia.userManagement.entity.Role;
import com.gosophia.userManagement.entity.RolePermission;
import com.gosophia.userManagement.entity.UserAccount;
import com.gosophia.userManagement.entity.UserInfo;

@ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class UTConfPrepareMapping extends
        AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * 对实体映射正确性进行测试
     * 
     * @throws Exception
     */
    @Test
    public void allClassMapping() throws Exception {
        Session session = sessionFactory.getCurrentSession();

        Map metadata = sessionFactory.getAllClassMetadata();
        for (Object o : metadata.values()) {
            EntityPersister persister = (EntityPersister) o;
            Object e = generateEntity(persister.getMappedClass(EntityMode.POJO));
            if (e == null) {
                continue;
            }
            session.save(e);
            session.flush();
            Serializable id = persister.getIdentifier(e, EntityMode.POJO);
            session.clear();
            session.get(persister.getMappedClass(EntityMode.POJO), id);
        }
    }

    protected Object generateEntity(Class entityClass) {
        Session session = sessionFactory.getCurrentSession();
        if(entityClass.equals(ParticipatingNameList.class)) {
        	ParticipatingNameList partic = new ParticipatingNameList();
            partic.setOrgName(UUID.randomUUID().toString().substring(0,16));
            partic.setPositionName(UUID.randomUUID().toString().substring(0,16));
            partic.setUserName(UUID.randomUUID().toString().substring(0,10));
            return partic;
        } else if(entityClass.equals(NotificationFeedbackList.class)){
        	NotificationFeedbackList notify = new NotificationFeedbackList();
        	notify.setIfAttend("YES");
        	UserInfo user = new UserInfo();
        	user.setUserName("userName");
        	user.setEmail("shu.com");
        	session.save(user);
        	session.flush();
        	notify.setUserInfo(user);
        	return notify;
        	
        }else if(entityClass.equals(ConfPreparationJobData.class)){
        	ConfPreparationJobData confP = new ConfPreparationJobData();
        	Document agenda = new Document();
        	agenda.setDocumentCategory("document");
        	agenda.setDocumentName("documentName");
        	agenda.setDocumentPath("documentPath");
        	session.save(agenda);

			confP.setAgenda(agenda);
        	Document checkInTable = new Document();
        	checkInTable.setDocumentCategory("document");
        	checkInTable.setDocumentName("documentName1");
        	checkInTable.setDocumentPath("documentPath1");
        	session.save(checkInTable);
			confP.setCheckInTable(checkInTable);
			confP.setConferenceScroll("条幅");
			confP.setFlowerNote("鲜花一大把");
			confP.setSnacks("小零食要准备");
			confP.setNotifactionNote("正式通知备注");
			confP.setPreNotifactionNote("预通知备注");
			confP.setSendNotifactionNote("发送通知备注");
			confP.setSendPreNotifactionNote("发送预通知备注");
			confP.setOtherNote("其他备注");
			
			UserInfo e = new UserInfo();
            e.setUserName("马小黑");
            e.setEmail("sohu.com");
            session.save(e);
            
            DataValue o = new DataValue();
            o.setOrderIndex(0);
            o.setIsEnabled(true);
            o.setValue(UUID.randomUUID().toString().substring(0, 5));
            o.setKey("001");
            session.save(o);
            Mission mission = new Mission();
            mission.setMissionCode("wdzb20110225004");
            mission.setMissionName("伟测试文档001");
            mission.setStartDate(new Date());
            mission.setScheduleEndDate(new Date());
            mission.setDescription("文档任务");
            mission.setMissionState("办理中");
            mission.setProcessInstenceId(15614L);
            mission.setUndertaker(e);
            mission.setCreator(e);
            mission.setUndertaker(e);
            mission.setMissionType(o);
            mission.setImportance(o);
            mission.setMissionDetailURL("getDocMissionDetailAction.action");
            session.save(mission);
           
        	Job job = new Job();
        	job.setJobState("未完成");
        	job.setHandlerURL("url");
        	job.setDetailURL("url");
        	job.setStartDate(new Date());
        	job.setScheduleEndDate(new Date());
        	job.setJobType("jobType");
        	job.setTokenName("/");
        	 job.setUndertaker(e);
             job.setSource(e);
        	 job.setMission(mission); 

        	session.save(job);
			confP.setJob(job);
//        	List<Document> materials = new ArrayList<Document>();
//        	materials.add(agenda);
//        	materials.add(checkInTable);
//			confP.setMaterials(materials);
//        	List<NotificationFeedbackList> notificationFeedbackLists = new ArrayList<NotificationFeedbackList>();
//        	NotificationFeedbackList notify = new NotificationFeedbackList();
//        	notify.setIfAttend("YES");
//        	UserInfo user = new UserInfo();
//        	user.setUserName("userName");
//        	session.save(user);
//        	notify.setUserInfo(user);
//        	session.save(notify);
//        	session.flush();
//        	notificationFeedbackLists.add(notify);
//        	confP.setNotificationFeedbackLists(notificationFeedbackLists);
        	List<ParticipatingNameList> participatingNameLists = new ArrayList<ParticipatingNameList>();
        	ParticipatingNameList partic = new ParticipatingNameList();
            partic.setOrgName(UUID.randomUUID().toString().substring(0, 16));
            partic.setPositionName(UUID.randomUUID().toString().substring(0, 16));
            partic.setUserName(UUID.randomUUID().toString().substring(0,10));
            session.save(partic);
            participatingNameLists.add(partic);
        	confP.setParticipatingNameLists(participatingNameLists);
        	session.flush();
        	return confP;
        	
        }else if(entityClass.equals(Document.class)){
        	Document doc = new Document();
        	doc.setDocumentCategory("document");
        	doc.setDocumentName("documentName");
        	doc.setDocumentPath("doc");
        	doc.setDocumentVersion("uuid");
            return doc;
        	
        }else{
        	return null;
        }
		
    }

}
